﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace _11_Generic
{
    internal class MyArray<T>
    {
        private T[] values;

        //private string[] strArr = { "123", "2131", "hello", "你好" };

        public int Count { get; private set; }
        public MyArray()
        {
            values = default;
            Count = 0;
        }
        // 增 删 查
        public void Add(T element) // 直接在末尾添加
        {
            T[] temp = new T[Count + 1];
            for (int i = 0; i < Count; i++)
            {
                temp[i] = values[i];
            }
            temp[Count++] = element;
            values = temp;
        }

        public void Insert(int index,T element)
        {
            // 在下标处(之前)插入元素
            if (index < 0 || index >= Count)
            {
                Console.WriteLine("下标越界");
                return;
            }
            T[] temp = new T[Count + 1];
            for (int i = 0;i < Count + 1; i++)
            {
                if( i < index)
                {
                    temp[i] = values[i];
                }
                else if (i == index)
                {
                    temp[i] = element;
                }
                else
                {
                    temp[i] = values[i - 1];
                }
            }
            Count++;
            values = temp;
        }

        public void Remove(int index) // 删除指定位置元素
        {
            if (index < 0 || index >= Count)
            {
                Console.WriteLine("下标越界");
                return;
            }
            T[] temp = new T[Count--];
            for(int i = 0;i < Count; i++)
            {
                if(i < index)
                {
                    temp[i] = values[i];
                }
                else
                {
                    temp[i] = values[i + 1];
                }
            }
            values = temp;
        }

        public void Delete()
        {
            // 删除最后的元素
            Remove(Count - 1);
        }

        public void Clear()
        {
            // 清空数组
            values = null;
            Count = 0;
        }

        public bool Contains(T element)
        {
            return values.Contains(element);
        }

        public T? Find(int index)
        {
            if (index < 0 || index >= Count)
            {
                Console.WriteLine("下标越界");
                return default;
            }
            return values[index];
        }

        // 索引器
        public T this[int index]
        {
            get
            {
                return Find(index);
            }
            set
            {
                values[index] = value;
            }
        }

        //public int this[string str]
        //{
        //    get
        //    {
        //        for (int i = 0; i < 4; i++)
        //        {
        //            if(str == strArr[i])
        //            {
        //                return i;
        //            }
        //        }
        //        return -1;
        //    }
        //}

        public void Print()
        {
            for(int i = 0;i < Count; i++)
            {
                Console.Write(values[i] + " ");
            }
            Console.WriteLine($"\nLength:{Count}");
        }
    }
}
